﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace AntC.AspNetCore.ApiEncryption.Internal.Streams
{
    /// <summary>
    /// api encrypt stream
    /// </summary>
    public class ApiEncryptStream : MemoryStream
    {
        /// <summary>
        /// async write to stream
        /// </summary>
        /// <param name="value">text.</param>
        /// <returns></returns>
        public async Task WriteAsync(string value)
        {
            var sw = new StreamWriter(this);
            await sw.WriteAsync(value);
            sw.Flush();
        }

        /// <summary>
        /// async write to stream
        /// </summary>
        /// <param name="value">text.</param>
        /// <param name="encoding">The value text encoding.</param>
        /// <returns></returns>
        public async Task WriteAsync(string value, Encoding encoding)
        {
            var sw = new StreamWriter(this, encoding);
            await sw.WriteAsync(value);
            sw.Flush();
        }

        /// <summary>
        /// async read from stream
        /// </summary>
        /// <returns></returns>
        public async Task<string> ReadStringAsync()
        {
            var sw = new StreamReader(this);
            return await sw.ReadToEndAsync();
        }

        /// <summary>
        /// async read from stream
        /// </summary>
        /// <param name="encoding">The text encoding for return value.</param>
        /// <returns></returns>
        public async Task<string> ReadStringAsync(Encoding encoding)
        {
            var sw = new StreamReader(this, encoding);
            return await sw.ReadToEndAsync();
        }
    }
}
